<?php
namespace app\dsadmin\model;
use think\Model;
use think\Db;

class TypeModel extends Model
{
    protected $name = 'type';

    public function getGongyouByWhere($map, $Nowpage, $limits)
    {
        return $this->where($map)->page($Nowpage, $limits)->order('id desc')->select();
    }

    public function insertGongyou($param)
    {
        try
        {
            $result = $this->allowField(true)->save($param);
            if (false === $result)
            {
                return ['code' => -1, 'data' => '', 'msg' => $this->getError()];
            }
            else
            {
                return ['code' => 1, 'data' => '', 'msg' => '添加成功'];
            }
        }
        catch(PDOException $e)
        {
            return ['code' => -2, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    public function updateGongyou($param)
    {
        try
        {
            $result = $this->allowField(true)->save($param, ['id' => $param['id']]);
            if (false === $result)
            {
                return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
            }
            else
            {
                return ['code' => 1, 'data' => '', 'msg' => '编辑成功'];
            }
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    public function getOneGongyou($id)
    {
        return $this->where('id', $id)->find();
    }

    public function delGongyou($id)
    {
        try
        {
            $this->where(['id' => ['in', $id]])->delete();
            return ['code' => 1, 'data' => '', 'msg' => '删除成功'];
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
}
